package com.qf.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qf.pojo.repVo.BaseResp;
import com.qf.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyZuulFilter extends ZuulFilter {

    private List urlList = new ArrayList<>();

    @Autowired
    JwtUtils jwtUtils;
    /**
     * 什么时候过滤请求
     * PRE 在请求路由之前被调用 使用过滤器实现身份的认证
     * ROUTE 载请求路由到对应的服务后调用
     * POST 载请求路由到对应的服务之后执行
     * ERROR 在其他阶段发生错误时执行
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    /**
     * 指定过滤器的执行顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER;
    }

    /**
     * 是否开启过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();
        if (urlList.contains(requestURI)){
            return false;
        }
        return true;
    }
    /*
    具体过滤器的业务代码
     */
    @Override
    public Object run() throws ZuulException {
       //1.获取到request请求的对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        //2.从请求中获取到头部中的token
        String token = request.getHeader("token");
        //判断token是否为null
        if (StringUtils.isEmpty(token)){
            //to'ken为null说明未登录,声明返回对象
            BaseResp baseResp = new BaseResp(0, "用户未登录！", null, null);
            //从currentContext 获取到response返回
            HttpServletResponse response = currentContext.getResponse();
           //设置编码格式，以及类型
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            try {
                //将声明的对象直接写回前端 以json方式
                response.getWriter().write(JSONObject.toJSONString(baseResp));
            } catch (IOException e) {
                e.printStackTrace();
            }
            //停止继续执行
            currentContext.setSendZuulResponse(false);
        }else {
            //进入该逻辑说明 token值不为Null,但是需要验证token是否正确
            Integer verfiy = jwtUtils.verfiy(token);
            //如果经过token的解密返回为0 说明token串解密失败！说明token串有问题
            if (verfiy==0){
                //写回前端，告诉前端该用户的token有问题，不能继续查看首页
                BaseResp baseResp = new BaseResp(0, "token认证失败！", null, null);
                HttpServletResponse response = currentContext.getResponse();
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                try {
                    response.getWriter().write(JSONObject.toJSONString(baseResp));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //停止继续执行
                currentContext.setSendZuulResponse(false);
            }
        }
        //继续执行 转发到对应的微服务中执行逻辑
        return null;
    }

    public MyZuulFilter(){
        //添加放行的请求
        urlList.add("/qflive-user/user/validataEmail");
        urlList.add("/qflive-user/user/validataloginName");
        urlList.add("/qflive-user/user/login");
        urlList.add("/qflive-user/user/registry");
        urlList.add("/qflive-user/user/getCode");
        urlList.add("/qflive-pay/pay/notifyResult");
    }
}
